<?php

namespace App\Http\Controllers\Mobile;

use App\Interfaces\CommentService;
use App\Interfaces\GoodsService;
use App\Interfaces\OrderService;
use App\Models\CommentPicture;
use App\Models\OrderGoods;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class CommentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,CommentService $commentService,OrderService $orderService)
    {
        $user = Auth::user();
        $order_id = $request->get('order_id');
        if($order_id){
            $commentGoods = $orderService->getOrderGoods($order_id);
            
        }else{

            $commentGoods = $commentService->getMyOrderGoods($user->id);
        }
        $commentOrderNum = $commentService->countMyOrderGoods($user->id);

        return view ('mobile.comment.index',compact('commentGoods','order_id'));
    }

    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function addComment(Request $request,CommentPicture $commentPicture,CommentService $commentService)
    {
        $user = Auth::user();
        //匿名设置
        if($request->get('niming')){
            $type = 1;
        }else{
            $type = 0;
        }
        $order_goods_id = $request->get('order_goods_id');
        $star = $request->get('star');
        $content = $request->has('content')?$request->get('content'):"";
        $picStr="";
        if(mb_strlen($content)>500){
            return response()->json(['status'=>0,'message'=>'评论字数不能超过500字']);

        }

        if($request->has('image_url')){
            $pictures = $request->get('image_url');

            if(count($pictures)>10){
                return response()->json(['status'=>0,'message'=>'上传图片不能超过10张']);
            }

            for($i=0;$i<count($pictures);$i++){
                if($picStr){
                    $picStr = $picStr.','.$pictures[$i];
                }else{
                    $picStr = $pictures[$i];
                }
            }
        }
        //dd($picStr);
        $orderGoods = OrderGoods::find($order_goods_id);
        if($orderGoods->comment_status==1){
            return response()->json(['status'=>0,'message'=>'该商品已评论']);
        }
        //dd($user->id);
        $comment =  $commentService->createComment($user->id,$order_goods_id,$star,$content,$label=null,$type,$picStr);
        if($comment){
            return response()->json(['status'=>1,'message'=>'评论成功']);
        }else{
            return response()->json(['status'=>0,'message'=>'评论失败']);
        }

    }

    public function show(Request $request,$order_goods_id,CommentService $commentService,GoodsService $goodsService)
    {
        $goods_id = $request->get('goods_id');
        $order_id = $request->get('order_id');
        $goods = $goodsService->get($goods_id);
        //dd($order_id);
        return view('mobile.comment.add',compact('goods','order_goods_id','order_id'));
    }


}
